package com.itheima.stock.controller;


import com.itheima.stock.pojo.domain.PermissionRespNodeVo;
import com.itheima.stock.pojo.entity.SysRole;
import com.itheima.stock.service.RolesService;
import com.itheima.stock.vo.req.AddRolesAndPermsReqVo;
import com.itheima.stock.vo.req.UpdatePermsByRoleIdReqVo;
import com.itheima.stock.vo.resp.PageResult;
import com.itheima.stock.vo.resp.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色处理器
 */
@RestController
@RequestMapping("/api")
public class RoleController {

    @Autowired
    private RolesService rolesService;

    /**
     * 分页查询当前角色信息
     *
     * @param map
     * @return
     */
    @PostMapping("/roles")
    public R<PageResult<SysRole>> getRolesPageInfo(@RequestBody Map<String, Integer> map) {
        return rolesService.getRolesPageInfo(map.get("pageNum"), map.get("pageSize"));
    }

    /**
     * 树状结构回显权限集合,底层通过递归获取权限数据集合
     */
    @GetMapping("/permissions/tree/all")
    public R<List<PermissionRespNodeVo>> getPermissionsTreeAll() {
        return rolesService.getPermissionsTreeAll();
    }

    /**
     * 添加角色和角色关联权限
     *
     * @param reqVo
     * @return
     */
    @PostMapping("/role")
    public R addRolesAndPerms(@RequestBody AddRolesAndPermsReqVo reqVo) {
        return rolesService.addRolesAndPerms(reqVo);
    }

    /**
     * 根据角色id查询权限
     *
     * @param roleId
     * @return
     */
    @GetMapping("/role/{roleId}")
    public R<List<Long>> getPermsByRoleId(@PathVariable String roleId) {
        return rolesService.getPermsByRoleId(roleId);
    }

    /**
     * 添加角色和角色关联权限
     *
     * @param reqVo
     * @return
     */
    @PutMapping("/role")
    public R updatePermsByRoleId(@RequestBody UpdatePermsByRoleIdReqVo reqVo) {
        return rolesService.updatePermsByRoleId(reqVo);
    }

    /**
     * 根据角色id删除角色信息
     *
     * @param roleId
     * @return
     */
    @DeleteMapping("/role/{roleId}")
    public R deleteRoles(@PathVariable String roleId) {
        return rolesService.deleteRoles(roleId);
    }

    /**
     * 更新用户的状态信息
     *
     * @param roleId
     * @param status
     * @return
     */
    @PostMapping("/role/{roleId}/{status}")
    public R updateRoleStatus(@PathVariable String roleId, @PathVariable Integer status) {
        return rolesService.updateRoleStatus(roleId, status);
    }
}